package com.esl.coordinator.image.test;

import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("serial")
class BWBufImagePanel extends JPanel {
	static final Logger LOG = LoggerFactory.getLogger(BWBufImagePanel.class);

	public static String BMP_PATH = "c:/a.bmp";

	static BufferedImage fBufferedImage = null;

	int fWidth = 172, fHeight = 72;
	byte[] data = null;
	int bytesPerRow = fWidth / 8 + (fWidth % 8 != 0 ? 1 : 0);

	/** Build a BufferedImage from a pixel array. **/
	void makeImage() {
		LOG.info("Size: " + fWidth + "x" + fHeight);

		byte[] bw = { (byte) 0, (byte) 0xff };
		IndexColorModel blackAndWhite = new IndexColorModel(1, // One bit per
																// pixel
				2, // Two values in the component arrays
				bw, // Red Components
				bw, // Green Components
				bw);// Blue Components

		// DataBuffer db = new DataBufferByte(data, data.length);

		// Get the writable raster so that data can be changed.
		// WritableRaster wr = Raster.createPackedRaster(db, fWidth, fHeight, 1,
		// null);

		// Create the BufferedImage
		// fBufferedImage = new BufferedImage(blackAndWhite, wr, true, null);

	}

	/** Draw the image on the panel. **/
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (fBufferedImage != null)
			g.drawImage(fBufferedImage, 0, 0, this);
	} // makeImage

	public static void main(String[] args) {
		File testBmp = new File(BMP_PATH);
		if (testBmp.exists()) {

			FileInputStream in;
			try {
				in = new FileInputStream(testBmp);
				BufferedImage bImage = ImageIO.read(in);
				fBufferedImage = bImage;

				JFrame frame = new JFrame("Test Sample");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				frame.setLayout(new GridLayout(1, 1));

				BWBufImagePanel panel = new BWBufImagePanel();
				panel.makeImage();
				frame.add(panel);

				frame.setSize(200, 100);
				frame.setVisible(true);
			} catch (FileNotFoundException e) {
				LOG.error(e.getMessage(), e);

			} catch (IOException e) {
				LOG.error(e.getMessage(), e);
			}
		}
	}
} // class BWBufImagePanel  